//第二部：创建各大功能

//1、加载dataBase文件
var db = require('./dataBase.js');

// ====================================课程管理========================
//获取课程管理 数据
classCourse = (req, res) => {
	var sql = 'SELECT * FROM class_course';
	var _data = [];
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '获取失败',
				'list': data
			})
		} else {
			res.send({
				code: 200,
				msg: '获取成功',
				'list': data
			})
		}
	}
	db.ConnectData(sql, _data, callBack)
}

// 课程管理-添加课程
addClassCourse = (req, res) => {
	var { serial_number, _name, cover, state_s, price, brief, teacher_init, phone, dispose_tim, up_tim, sech_tim } = req.query;

	var _data = [serial_number, _name, cover, state_s, price, brief, teacher_init, phone, dispose_tim, up_tim, sech_tim];

	var sql = 'SELECT * FROM class_course WHERE name="' + _name + '"';

	var callBack = (err, data) => {
		console.log(data)
		if (err) {
			res.send({
				code: 404,
				msg: '添加失败'
			})
		} else {
			// console.log(data)
			if (data[1] != _name) {
				//添加语句
				var _sql = 'INSERT INTO class_course (serial_number,name,cover,state_s,price,brief,teacher_init,phone,dispose_tim,up_tim,sech_tim) VALUE (?,?,?,?,?,?,?,?,?,?,?)';

				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '信息已存在'
						})
					} else {
						res.send({
							code: 1,
							msg: '添加成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			} else {
				res.send({
					code: 0,
					msg: '数据已存在'
				})
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}

// 课程管理—————删除功能
delClassCourse = (req, res) => {
	var serNum = req.query.serNum;

	var _data = [serNum];

	//设置sql语句
	var sql = 'SELECT * FROM class_course WHERE name="' + serNum + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作失败'
			})
		} else {
			//删除操作
			if (data[0].name == serNum) {

				var _sql = 'DELETE FROM class_course WHERE name=?';
				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '删除失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '删除成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}

// 课程管理-修改
editClassCourse = (req, res) => {
	var { serial_number, name, cover, state_s, price } = req.query;
	// console.log(id)
	var _data = [serial_number, name, cover, state_s, price];

	var sql = 'SELECT * FROM class_course WHERE serial_number="' + serial_number + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作错误'
			})
		} else {
			// console.log(id)
			if (data[0].serial_number == serial_number) {

				var _sql = 'UPDATE class_course SET serial_number=?,name=?,cover=?,state_s=?,price=? WHERE serial_number="' + serial_number + '"';

				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '修改失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '修改成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			} else {
				res.send({
					code: 3,
					msg: '修改失败'
				})
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}

// 课程管理-查询
sechClassCourse = (req, res) => {
	var { sech_one, sech_two, sech_three } = req.query;

	var _data = [sech_one, sech_two, sech_three]
	// console.log('查询' + sech_three)
	// console.log('查询' + sech_two)
	// console.log('查询' + sech_three)
	var sql = 'SELECT serial_number,name,cover,state_s,price,brief,teacher_init,up_tim,phone,sech_tim FROM class_course WHERE state_s="' + sech_one + '"  OR up_tim="' + sech_two + '" OR name="' + sech_three + '"';
	// console.log()
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 0,
				msg: '没有查询的数据',
				'list': data
			})
		} else {
			res.send({
				code: 1,
				msg: '查询成功',
				'list': data
			})
		}
	}
	db.ConnectData(sql, _data, callBack);
}
// ===============================================订单管理-订单-------------------=--------

//获取订单管理-订单 数据
odOrder = (req, res) => {
	var sql = 'SELECT * FROM class_course';
	var _data = [];
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '获取失败',
				'list': data
			})
		} else {
			res.send({
				code: 200,
				msg: '获取成功',
				'list': data
			})
		}
	}
	db.ConnectData(sql, _data, callBack)
}
// 订单管理-订单查询
sechOdOrder = (req, res) => {
	var { sech_one, sech_two, sech_three } = req.query;

	var _data = [sech_one, sech_two, sech_three]
	var sql = 'SELECT serial_number,name,cover,state_s,price,brief,teacher_init,up_tim,phone,sech_tim FROM class_course WHERE state_s="' + sech_one + '" OR up_tim="' + sech_two + '" OR name="' + sech_three + '" OR phone="' + sech_three + '"';
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 0,
				msg: '没有查询的数据',
				'list': data
			})
		} else {
			res.send({
				code: 1,
				msg: '查询成功',
				'list': data
			})
		}
	}
	db.ConnectData(sql, _data, callBack);
}

// ================================================订单管理-退款==========================================
//获取订单管理-退款 数据
odRefund = (req, res) => {
	var sql = 'SELECT * FROM class_course';
	var _data = [];
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '获取失败',
				'list': data
			})
		} else {
			res.send({
				code: 200,
				msg: '获取成功',
				'list': data
			})
		}
	}
	db.ConnectData(sql, _data, callBack)
}
// 订单管理-退款查询
sechOdRefund = (req, res) => {

	var { sech_one, sech_two } = req.query;

	var _data = [sech_one, sech_two]
	var sql = 'SELECT serial_number,name,cover,state_s,price,brief,teacher_init,up_tim,phone,sech_tim FROM class_course WHERE up_tim="' + sech_one + '" OR phone="' + sech_two + '" OR name="' + sech_two + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 0,
				msg: '没有查询的数据',
				'list': data
			})
		} else {
			res.send({
				code: 1,
				msg: '查询成功',
				'list': data
			})
		}
	}
	db.ConnectData(sql, _data, callBack);
}

// ===========================================用户管理-——学员==================================
// 获取用户管理-学员 数据
userStudent = (req, res) => {
	var sql = 'SELECT * FROM user_student';
	var _data = [];
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '获取失败',
				'list': data
			})
		} else {
			res.send({
				code: 200,
				msg: '获取成功',
				'list': data
			})
		}
	}
	db.ConnectData(sql, _data, callBack)
}
// 用户管理-学员————添加学员
addUserStudent = (req, res) => {
	var {
		stu_ID,
		name,
		phone,
		stu_pyp,
		up_pyp,
		stu_tim,
		state_s
	} = req.query;

	var _data = [stu_ID, name, phone, stu_pyp, up_pyp, stu_tim,state_s];
	console.log(_data)
	var sql = 'SELECT * FROM user_student WHERE name="' + name + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '添加失败'
			})
		} else {
			// console.log(data)
			if (data[0] != name) {
				//添加语句
				var _sql = 'INSERT INTO user_student (stu_ID, name, phone, stu_pyp,up_pyp,stu_tim,state_s) VALUE (?,?,?,?,?,?,?)';

				var _callBack = (err, result) => {
					if (err) {
						console.log(err)
						res.send({
							code: 0,
							msg: '信息已存在'
						})
					} else {
						res.send({
							code: 1,
							msg: '添加成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			} else {
				res.send({
					code: 0,
					msg: '数据已存在'
				})
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}
// 用户管理-学员————删除功能
delUserStudent = (req, res) => {
	var serNum = req.query.serNum;

	var _data = [serNum];

	//设置sql语句
	var sql = 'SELECT * FROM user_student WHERE name="' + _data[0] + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作失败'
			})
		} else {
			//删除操作
			if (data[0].name == serNum) {
				var _sql = 'DELETE FROM user_student WHERE name=?';
				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '删除失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '删除成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}

// 用户管理-学员修改
editUserStudent = (req, res) => {
	var { stu_ID, name, phone, stu_pyp, up_pyp, state_s } = req.query;
	// console.log(id)
	var _data = [stu_ID, name, phone, stu_pyp, up_pyp,state_s];

	var sql = 'SELECT * FROM user_student WHERE stu_ID="' + stu_ID + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作错误'
			})
		} else {
			if (data[0].stu_ID == stu_ID) {

				var _sql = 'UPDATE user_student SET stu_ID=?,name=?,phone=?,stu_pyp=?,up_pyp=?,state_s=? WHERE stu_ID="' + stu_ID + '"';

				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '修改失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '修改成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			} else {
				res.send({
					code: 3,
					msg: '修改失败'
				})
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}
// 用户管理-学员查询
sechUserStudent = (req, res) => {
	var { sech_one, sech_two, sech_three } = req.query;

	var _data = [sech_one, sech_two,sech_three]
	var sql = 'SELECT name,phone,stu_pyp,up_pyp,stu_tim,state_s FROM user_student WHERE stu_tim="' + sech_one + '" OR state_s="' + sech_two + '" OR phone="' + sech_three + '" OR name="' + sech_three + '"';
	console.log(_data)
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 0,
				msg: '没有查询的数据',
				'list': data
			})
		} else {
			res.send({
				code: 1,
				msg: '查询成功',
				'list': data
			})
		}
	}
	db.ConnectData(sql, _data, callBack);
}
// ============================================= 用户管理-讲师=========================================
// 获取用户管理-讲师 数据
userTeacher = (req, res) => {
	var sql = 'SELECT * FROM user_teacher';
	var _data = [];
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '获取失败',
				'list': data
			})
		} else {
			res.send({
				code: 200,
				msg: '获取成功',
				'list': data
			})
		}
	}
	db.ConnectData(sql, _data, callBack)
}
// 用户管理-讲师——添加讲师
addUserTeacher = (req, res) => {
	var {
		lec_name,
		lec_head,
		lec_brief,
		detail,
		add_tim
	} = req.query;

	var _data = [lec_name, lec_head, lec_brief, detail, add_tim];

	var sql = 'SELECT * FROM user_teacher WHERE lec_name="' + lec_name + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '添加失败'
			})
		} else {
			// console.log(data)
			if (data[0] != lec_name) {
				//添加语句
				var _sql = 'INSERT INTO user_teacher (lec_name,lec_head,lec_brief,detail,add_tim) VALUE (?,?,?,?,?)';

				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '信息已存在'
						})
					} else {
						res.send({
							code: 1,
							msg: '添加成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			} else {
				res.send({
					code: 0,
					msg: '数据已存在'
				})
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}
// 用户管理-讲师————删除功能
delUserTeacher = (req, res) => {
	var lecName = req.query.lecName;

	var _data = [lecName];

	//设置sql语句
	var sql = 'SELECT * FROM user_teacher WHERE lec_name="' + _data[0] + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作失败'
			})
		} else {
			//删除操作
			if (data[0].lec_name == lecName) {
				var _sql = 'DELETE FROM user_teacher WHERE lec_name=?';
				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '删除失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '删除成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}
// 用户管理-讲师修改
editUserTeacher = (req, res) => {
	var { lec_name, lec_head, lec_brief, detail, add_tim } = req.query;
	// console.log(id)
	var _data = [lec_name, lec_head, lec_brief, detail, add_tim];

	var sql = 'SELECT * FROM user_teacher WHERE lec_name="' + lec_name + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作错误'
			})
		} else {
			if (data[0].lec_name == lec_name) {

				var _sql = 'UPDATE user_teacher SET lec_name=?,lec_head=?,lec_brief=?,detail=?,add_tim=? WHERE lec_name="' + lec_name + '"';

				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '修改失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '修改成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			} else {
				res.send({
					code: 3,
					msg: '修改失败'
				})
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}
// 用户管理-讲师查询
sechUserTeacher = (req, res) => {
	var { sech_one, sech_two } = req.query;

	var _data = [sech_one, sech_two]
	var sql = 'SELECT lec_name,add_tim FROM user_teacher WHERE add_tim="' + sech_one + '" OR lec_name="' + sech_two + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 0,
				msg: '没有查询的数据',
				'list': data
			})
		} else {
			res.send({
				code: 1,
				msg: '查询成功',
				'list': data
			})
		}
	}
	db.ConnectData(sql, _data, callBack);
}

// = ===================================================获取资讯管理-轮播图================================
// 获取资讯管=理-轮播图 数据
infCarousel = (req, res) => {
	var sql = 'SELECT * FROM inf_carousel';
	var _data = [];

	//设置回调函数
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '获取失败',
				'list': data
			})
		} else {
			res.send({
				code: 200,
				msg: '获取成功',
				'list': data
			})
		}
	}
	db.ConnectData(sql, _data, callBack)
}

// 资讯管理—轮播图————删除功能
delInfCarousel = (req, res) => {
	var serNum = req.query.serNum;

	var _data = [serNum];

	//设置sql语句
	var sql = 'SELECT * FROM inf_carousel WHERE stu_name="' + _data[0] + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作失败'
			})
		} else {
			//删除操作
			if (data[0].stu_name == serNum) {
				var _sql = 'DELETE FROM inf_carousel WHERE stu_name=?';
				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '删除失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '删除成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}

addInfCarousel = (req, res) => {
	var { stu_name,stu_pict,stu_explain,stu_link,add_tim } = req.query;

	var _data = [stu_name,stu_pict,stu_explain,stu_link,add_tim];
	var sql = 'SELECT * FROM inf_carousel WHERE stu_name="' + stu_name + '"';
	// console.log(_data)
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '添加失败'
			})
		} else {
			// console.log(data[0]);
			if (data[0] != stu_name) {
				//添加语句
				var _sql = 'INSERT INTO inf_carousel (stu_name,stu_pict,stu_explain,stu_link,add_tim) VALUES (?,?,?,?,?)';
				var _callBack = (err, result) => {
					if (err) {
						// console.log(err);
						res.send({
							code: 0,
							msg: '信息已存在'
						})
					} else {
						res.send({
							code: 1,
							msg: '添加成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			} else {
				res.send({
					code: 0,
					msg: '数据已存在'
				})
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}

// 资讯管理-轮播图修改
editInfCarousel = (req, res) => {
	var { name, picture, explain, pic_link, add_tim } = req.query;
	// console.log(id)
	var _data = [name, picture, explain, pic_link, add_tim];

	var sql = 'SELECT * FROM inf_carousel WHERE name="' + name + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作错误'
			})
		} else {
			if (data[0].name == name) {

				var _sql = 'UPDATE inf_carousel SET name=?,picture=?,explain=?,pic_link=?,add_tim=? WHERE name="' + name + '"';

				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '修改失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '修改成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			} else {
				res.send({
					code: 3,
					msg: '修改失败'
				})
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}
// ================================================== 资讯管理-文章========================================
// 获取质询管理-文章 数据
infArticle = (req, res) => {
	var sql = 'SELECT * FROM inf_article';
	var _data = [];

	//设置回调函数
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '获取失败',
				'list': data
			})
		} else {
			res.send({
				code: 200,
				msg: '获取成功',
				'list': data
			})
		}
	}
	db.ConnectData(sql, _data, callBack)
}
// 资讯管理-文章————删除功能
delInfArticle = (req, res) => {
	var serNum = req.query.serNum;

	var _data = [serNum];

	//设置sql语句
	var sql = 'SELECT * FROM inf_article WHERE title="' + _data[0] + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作失败'
			})
		} else {
			//删除操作
			if (data[0].title == serNum) {
				var _sql = 'DELETE FROM inf_article WHERE title=?';
				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '删除失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '删除成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}
// 资讯管理-文章——添加文章
addInfArticle = (req, res) => {
	var {
		title,
		cover,
		inf_describe,
		text_content,
		add_tim
	} = req.query;

	var _data = [title, cover, inf_describe, text_content, add_tim];

	var sql = 'SELECT * FROM inf_article WHERE title="' + title + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '添加失败'
			})
		} else {
			// console.log(data)
			if (data[0] != title) {
				//添加语句
				var _sql = 'INSERT INTO inf_article (title,cover,inf_describe,text_content,add_tim) VALUE (?,?,?,?,?)';

				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '信息已存在'
						})
					} else {
						res.send({
							code: 1,
							msg: '添加成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			} else {
				res.send({
					code: 0,
					msg: '数据已存在'
				})
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}
// 资讯管理-文章修改
editInfArticle = (req, res) => {
	var { title, cover, describe, text_content, add_tim } = req.query;
	// console.log(id)
	var _data = [title, cover, describe, text_content, add_tim];

	var sql = 'SELECT * FROM inf_article WHERE title="' + title + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作错误'
			})
		} else {
			if (data[0].title == title) {

				var _sql = 'UPDATE inf_article SET title=?,cover=?,describe=?,text_content=?,add_tim=? WHERE title="' + title + '"';

				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '修改失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '修改成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			} else {
				res.send({
					code: 3,
					msg: '修改失败'
				})
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}

// ==============================================促销管理-秒杀活动========================================
// 获取促销管理-秒杀活动 数据
promKill = (req, res) => {
	var sql = 'SELECT * FROM prom_k';
	var _data = [];

	//设置回调函数
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '获取失败',
				'list': data
			})
		} else {
			res.send({
				code: 200,
				msg: '获取成功',
				'list': data
			})
		}
	}
	db.ConnectData(sql, _data, callBack)
}
// 促销管理-秒杀活动————删除动能
delPromKill = (req, res) => {
	var k_title = req.query.k_title;

	var _data = [k_title];

	//设置sql语句
	var sql = 'SELECT * FROM prom_k WHERE k_title="' + k_title + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作失败'
			})
		} else {
			//删除操作
			if (data[0].k_title == k_title) {
				var _sql = 'DELETE FROM prom_k WHERE k_title=?';
				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '删除失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '删除成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}
// 促销管理-秒杀活动——添加秒杀活动
addPromKill = (req, res) => {
	var {k_title,k_cover,k_state,end_tim,start_tim,c_num} = req.query;

	var _data = [k_title,k_cover,k_state,end_tim,start_tim,c_num];

	var sql = 'SELECT * FROM prom_k WHERE k_title="' + k_title + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '添加失败'
			})
		} else {
			// console.log(data)
			if (data[0] != k_title) {
				//添加语句
				var _sql = 'INSERT INTO prom_k (title,cover,state,end_tim,start_tim,num) VALUES (?,?,?,?,?,?)';

				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '信息已存在'
						})
					} else {
						res.send({
							code: 1,
							msg: '添加成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			} else {
				res.send({
					code: 0,
					msg: '数据已存在'
				})
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}
// 促销管理-秒杀活动修改
editPromKill = (req, res) => {
	var { kill_num, kill_title, kill_cover, kill_state, end_tim, start_tim } = req.query;
	// console.log(id)
	var _data = [kill_num, kill_title, kill_cover, kill_state, end_tim, start_tim];

	var sql = 'SELECT * FROM prom_k WHERE kill_num="' + kill_num + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作错误'
			})
		} else {
			if (data[0].kill_num == kill_num) {

				var _sql = 'UPDATE prom_k SET kill_num=?,kill_title=?,kill_cover=?,kill_state=?,end_tim=?,start_tim=? WHERE kill_num="' + kill_num + '"';

				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '修改失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '修改成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			} else {
				res.send({
					code: 3,
					msg: '修改失败'
				})
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}
// =================================================================促销管理-秒杀活动-课程设置=================================================
// 获取促销管理-秒杀活动-课程设置 数据
promCourseSet = (req, res) => {
	var sql = 'SELECT * FROM prom_kill';
	var _data = [];

	//设置回调函数
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '获取失败',
				'list': data
			})
		} else {
			res.send({
				code: 200,
				msg: '获取成功',
				'list': data
			})
		}
	}
	db.ConnectData(sql, _data, callBack)
}
// 促销管理-秒杀活动-课程设置————删除功能
delPromCourseSet = (req, res) => {
	var killNum = req.query.killNum;

	var _data = [killNum];

	//设置sql语句
	var sql = 'SELECT * FROM prom_kill WHERE kill_num="' + _data[0] + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作失败'
			})
		} else {
			//删除操作
			if (data[0].kill_num == killNum) {
				var _sql = 'DELETE FROM prom_kill WHERE kill_num=?';
				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '删除失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '删除成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}

// 促销管理-秒杀活动-课程设置修改
editPromCourseSet = (req, res) => {
	var { kill_num, kill_title, kill_cover, kill_state, end_tim, start_tim } = req.query;
	// console.log(id)
	var _data = [kill_num, kill_title, kill_cover, kill_state, end_tim, start_tim];

	var sql = 'SELECT * FROM prom_kill WHERE kill_num="' + kill_num + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作错误'
			})
		} else {
			if (data[0].kill_num == kill_num) {

				var _sql = 'UPDATE prom_kill SET kill_num=?,kill_title=?,kill_cover=?,kill_state=?,end_tim=?,start_tim=? WHERE kill_num="' + kill_num + '"';

				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '修改失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '修改成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			} else {
				res.send({
					code: 3,
					msg: '修改失败'
				})
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}
// ================================================================促销管理-优惠卷================================================
// 获取促销管理-优惠卷 数据
promCoupon = (req, res) => {
	var sql = 'SELECT * FROM prom_coupon';
	var _data = [];

	//设置回调函数
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '获取失败',
				'list': data
			})
		} else {
			res.send({
				code: 200,
				msg: '获取成功',
				'list': data
			})
		}
	}
	db.ConnectData(sql, _data, callBack)
}

// 促销管理-优惠卷——添加优惠卷
addPromCoupon = (req, res) => {
	var {
		prom_num,
		name,
		use_scope,
		prom_sill,
		prom_day,
		face_value,
		state_s,
		state_tim,
		prom_type,
		prom_number,
		end_tim,
	} = req.query;

	var _data = [
		prom_num,
		name,
		use_scope,
		prom_sill,
		prom_day,
		face_value,
		state_s,
		state_tim,
		prom_type,
		prom_number,
		end_tim,
	];

	var sql = 'SELECT * FROM prom_coupon WHERE name="' + name + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '添加失败'
			})
		} else {
			console.log(data)
			if (data[1] != name) {
				//添加语句
				var _sql = 'INSERT INTO prom_coupon (prom_num,name,use_scope,prom_sill,prom_day,face_value,state_s,state_tim,prom_type,prom_number,end_tim) VALUE (?,?,?,?,?,?,?,?,?,?,?)';

				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '信息已存在'
						})
					} else {
						res.send({
							code: 1,
							msg: '添加成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			} else {
				res.send({
					code: 0,
					msg: '数据已存在'
				})
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}
// 促销管理-优惠卷————删除功能
delPromCoupon = (req, res) => {
	var _name = req.query.name;

	var _data = [_name];

	//设置sql语句
	var sql = 'SELECT * FROM prom_coupon WHERE name="' + _name + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作失败'
			})
		} else {
			//删除操作
			if (data[0].name == _name) {
				var _sql = 'DELETE FROM prom_coupon WHERE name=?';
				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '删除失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '删除成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}

// 促销管理-优惠卷查询
sechPromCoupon = (req, res) => {
	var { sech_one, sech_two, sech_three } = req.query;

	var _data = [sech_one, sech_two, sech_three]
	var sql = 'SELECT prom_num,name,use_scope,prom_sill,prom_day,face_value,state_s,state_tim,prom_type,prom_number,end_tim FROM prom_coupon WHERE name="' + sech_one + '" OR state_tim="' + sech_two + '" OR end_tim="' + sech_two + '" OR face_value="' + sech_three + '" OR prom_type="' + sech_three + '"';
	console.log('查询' + sech_one)
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 0,
				msg: '没有查询的数据',
				'list': data
			})
		} else {
			res.send({
				code: 1,
				msg: '查询成功',
				'list': data
			})
		}
	}
	db.ConnectData(sql, _data, callBack);
}
// 促销管理-优惠卷修改
editPromCoupon = (req, res) => {
	var { prom_num, name, prom_sill, use_scope, prom_day, face_value, state_s } = req.query;
	// console.log(id)
	var _data = [prom_num, name, prom_sill, use_scope, prom_day, face_value, state_s];

	var sql = 'SELECT * FROM prom_coupon WHERE prom_num="' + prom_num + '"';

	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作错误'
			})
		} else {
			if (data[0].prom_num == prom_num) {

				var _sql = 'UPDATE prom_coupon SET prom_num=?,name=?,prom_sill=?,use_scope=?,prom_day=?,face_value=?,state_s=? WHERE prom_num="' + prom_num + '"';

				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '修改失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '修改成功'
						})
					}
				}
				db.ConnectData(_sql, _data, _callBack)
			} else {
				res.send({
					code: 3,
					msg: '修改失败'
				})
			}
		}
	}
	db.ConnectData(sql, [], callBack)
}
// ===========================================================注册功能======================================
getRegister = (req, res) => {
	//使用mysql语句（用ES6中的对象解构）
	var {
		user,
		pass
	} = req.body

	//定义赋值请求参数
	var _data = [user, pass]
	// console.log(_data)
	//设置sql语句（查看是否已注册）
	var sql = 'SELECT * FROM login WHERE user="' + user + '"';

	//设置回调函数
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 403,
				masg: '操作错误'
			})
		} else {
			if (data[0] === undefined) { //表示没有注册
				//插入字段
				var _sql = 'INSERT INTO login (user, pass) VALUE (?,?)';

				//设置回调函数
				var _callBack = (err, result) => {
					if (err) {
						res.send({
							code: 0,
							msg: '注册失败'
						})
					} else {
						res.send({
							code: 1,
							msg: '注册成功'
						})
					}
				}
				//执行插入语句
				db.ConnectData(_sql, _data, _callBack)
			} else {
				res.send({
					code: 2,
					msg: '账号已存在'
				})
			}
		}
	}
	//执行查询语句
	db.ConnectData(sql, [], callBack)
}

// ======================================================================登录功能===============================
getLogin = (req, res) => {
	var {
		user,
		pass
	} = req.body;
	var _data = [user, pass];
	var sql = 'SELECT pass FROM login WHERE user="' + user + '"';
	var callBack = (err, data) => {
		if (err) {
			res.send({
				code: 404,
				msg: '操作错误',
				isOk: 0 //此字段用来判断是否登入
			})
		} else {
			if (data[0] === undefined) { //表示数据库中没有这个账号
				res.send({
					code: 304,
					msg: '用户不存在',
					isOk: 2,
					'list': data
				})
			} else {
				//数据表中有这个账号，然后判断密码是否正确
				if (data[0].pass == _data[1]) { //密码正确
					res.send({
						code: 200,
						msg: '登入成功',
						isOk: 1,
						'list': data
					})
				} else { //表示密码错误
					res.send({
						code: 300,
						msg: '密码错误',
						isOk: 3,
						'list': data
					})
				}
			}
		}
	}
	db.ConnectData(sql, _data, callBack)
}

//3、暴露功能函数===========================================================================
module.exports = {
	// ======================== 登录注册=============
	// 注册功能
	getRegister,
	// 登录功能
	getLogin,
	// ---------------------渲染全部------------------
	// 课程管理
	classCourse,
	// 订单管理-订单
	odOrder,
	// 订单管理-退款
	odRefund,
	// 用户管理-学员
	userStudent,
	// 用户管理-讲师
	userTeacher,
	// 资讯管理-轮播图
	infCarousel,
	// 质询管理-文章
	infArticle,
	// 促销管理-秒杀活动
	promKill,
	// 促销管理-秒杀活动-课程设置
	promCourseSet,
	// 促销管理-优惠卷
	promCoupon,
	// ---------------------------------删除----------------------
	// 课程管理—————删除功能
	delClassCourse,
	// 用户管理-学员————删除功能
	delUserStudent,
	// 用户管理-讲师————删除功能
	delUserTeacher,
	// 资讯管理—轮播图————删除功能
	delInfCarousel,
	// 资讯管理-文章————删除功能
	delInfArticle,
	// 促销管理-秒杀活动————删除动能
	delPromKill,
	// 促销管理-秒杀活动-课程设置————删除功能
	delPromCourseSet,
	//  促销管理-优惠卷————删除功能
	delPromCoupon,
	// ----------------------------添加----------------------
	// 课程管理-添加课程
	addClassCourse,
	// 用户管理-讲师——添加讲师
	addUserTeacher,
	// 用户管理-学院——添加学员
	addUserStudent,
	// 资讯管理-轮播图——添加轮播图
	addInfCarousel,
	// 资讯管理-文章——添加文章
	addInfArticle,
	// 促销管理-秒杀活动——添加秒杀活动
	addPromKill,
	// 促销管理-优惠卷——添加优惠卷
	addPromCoupon,
	//---------------------------- 查询--------------------
	// 课程管理-查询
	sechClassCourse,
	// 订单管理-订单查询
	sechOdOrder,
	// 订单管理-退款查询
	sechOdRefund,
	// 用户管理-学员查询
	sechUserStudent,
	// 用户管理-讲师查询
	sechUserTeacher,
	// 促销管理-优惠卷查询
	sechPromCoupon,
	// ----------------------------修改--------------------
	// 课程管理-修改
	editClassCourse,
	// 用户管理-学员修改
	editUserStudent,
	// 用户管理-讲师修改
	editUserTeacher,
	// 资讯管理-轮播图修改
	editInfCarousel,
	// 资讯管理-文章修改
	editInfArticle,
	// 促销管理-秒杀活动修改
	editPromKill,
	// 促销管理-秒杀活动-课程设置修改
	editPromCourseSet,
	// 促销管理-优惠卷修改
	editPromCoupon
}
